import { IArticle } from "@/store";
import request_ from ".";
import _request from ".";

/**
 * 创建文章
 * @param {String} - title 文章标题
 * @param {String} - desc 文章描述/摘要
 * @param {Array} - tags 文章的标签 最多5个
 * @param {String} - content 文章内容
 * @param {String} - type 文章类型
 * @param {String} - cover 文章封装图
 */

export const createArticleService = (data: any) => {
  console.log("创建文章");

  return _request.post({
    url: "/article",
    data: {
      ...data
    }
  });
};

/**
 * 获取所有文章
 * @params {}
 * */
// "/article"+ "?offset=0&limit=2"
export const getAllArticleService = (
  sort?: any,
  offset?: number,
  limit?: number
) => {
  return _request.get({
    url: `${limit ? `/article?offset=${offset}&limit=${limit}` : "/article"}`,
    params: { sort, timestamp: new Date().getTime() }
  });
};

/**
 *  模糊搜索文章
 * @parma {String} - key 搜索的关键字
 * */

export const getSearchArticleService = (key: string, sort: number) => {
  return _request.get({ url: "/article/search?key=" + key, params: { sort } });
};

/**
 * 获取所有标签
 * @param {}
 * */

export const getArticleTagsService = () => {
  return _request.get({ url: "/tags" });
};

/**
 * 添加标签
 * */
export const addTagService = (data: { tagName: string; uid: number }) => {
  return _request.post({ url: "/tags", data: { ...data } });
};

/**
 * 根据文章ID获取文章内容
 * @param {String} - id 文章的ID
 *
 * */

export const getArticleByID = (id: string) => {
  return _request.get({ url: "/article/" + id });
};

/**
 * 增加文章的阅读量
 *
 * @param {String} - id
 *
 * */

export const addReadingService = (id: number) => {
  return request_.patch({ url: "/article/addreading", data: { id } });
};

/**
 * 更新文章内容
 *
 * @parma {} - data 文章数据
 * */
export const udpateArticle = (data: IArticle) => {
  return request_.patch({ url: "/article", data });
};

/**
 * 删除文章
 * @param {Integer} - id 文章ID
 * */

export const deleteArticle = (id: number) => {
  return request_.delete({ url: "/article", data: { id } });
};

/**
 * 获取用户文章
 * @param {} - id 用户ID
 * */
export const getArticleByUserId = (id: any) => {
  return request_.get({ url: "/article/user/" + id });
};

/**
 * 根据Tag获取该类文章
 * @param {String} - tag 标签名
 * */

export const getTagArticle = (tag: string, sort: number) => {
  return request_.get({
    url: "/article/tag",
    params: {
      tag,
      sort,
      timestamp: new Date().getTime()
    }
  });
};
